package com.xqboss.apps.service.nft;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.nft.ExclusiveBuy;
import com.xqboss.apps.dto.nft.NftExclusiveBuyDTO;
import com.xqboss.apps.dto.nft.NftExclusiveBuyMemberDTO;
import com.xqboss.apps.mapper.nft.ExclusiveBuyMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 专属购 服务实现类
 * </p>
 *
 * @author HaoLun
 * @since 2022-09-22
 */
@Service
public class ExclusiveBuyService extends ServiceImpl<ExclusiveBuyMapper, ExclusiveBuy> {


    public void add(List<NftExclusiveBuyDTO> nftExclusiveBuyDTOS, Long nftId, Integer exclusiveType) {

        this.remove(new LambdaQueryWrapper<ExclusiveBuy>().eq(ExclusiveBuy::getNftId, nftId));
        if (nftExclusiveBuyDTOS == null || nftExclusiveBuyDTOS.isEmpty()) {
            return;
        }
        Integer groupId = 0;
        List<ExclusiveBuy> exclusiveBuys = new ArrayList<>();
        for (NftExclusiveBuyDTO nftExclusiveBuyDTO : nftExclusiveBuyDTOS) {
            groupId = groupId + 1;
            for (NftExclusiveBuyMemberDTO member : nftExclusiveBuyDTO.getMembers()) {
                ExclusiveBuy exclusiveBuy = new ExclusiveBuy();
                exclusiveBuy.setBuyTimes(member.getBuyTimes());
                exclusiveBuy.setInitTimes(member.getInitTimes());
                exclusiveBuy.setUserId(member.getUserId());
                exclusiveBuy.setBeforeMinute(nftExclusiveBuyDTO.getBeforeMinute());
                exclusiveBuy.setGroupId(groupId);
                exclusiveBuy.setNftId(nftId);
                exclusiveBuy.setExclusiveType(exclusiveType);
                exclusiveBuys.add(exclusiveBuy);
            }
        }
        this.saveBatch(exclusiveBuys);

    }

    /**
     * 获取专属购数据
     *
     * @param nftID
     */
    public List<NftExclusiveBuyDTO> getDetail(Long nftID, Integer exclusiveType) {
        List<ExclusiveBuy> exclusiveBuys = this.list(new LambdaQueryWrapper<ExclusiveBuy>().eq(ExclusiveBuy::getExclusiveType, exclusiveType).eq(ExclusiveBuy::getNftId, nftID).orderByAsc(ExclusiveBuy::getGroupId));
        List<NftExclusiveBuyDTO> nftExclusiveBuyDTOS = new ArrayList<>();
        if (exclusiveBuys.isEmpty()) {
            return nftExclusiveBuyDTOS;
        }
        List<Integer> groupIds = exclusiveBuys.stream().map(ExclusiveBuy::getGroupId).distinct().collect(Collectors.toList());


        for (Integer groupId : groupIds) {
            List<ExclusiveBuy> exclusiveBuyList = exclusiveBuys.stream().filter(exclusiveBuy -> exclusiveBuy.getGroupId().equals(groupId)).collect(Collectors.toList());
            NftExclusiveBuyDTO nftExclusiveBuyDTO = new NftExclusiveBuyDTO();
            nftExclusiveBuyDTO.setBeforeMinute(exclusiveBuyList.get(0).getBeforeMinute());
            List<NftExclusiveBuyMemberDTO> memberDTOS = new ArrayList<>();
            for (ExclusiveBuy exclusiveBuy : exclusiveBuyList) {
                NftExclusiveBuyMemberDTO exclusiveBuyMemberDTO = new NftExclusiveBuyMemberDTO();
                exclusiveBuyMemberDTO.setUserId(exclusiveBuy.getUserId());
                exclusiveBuyMemberDTO.setBuyTimes(exclusiveBuy.getBuyTimes());
                exclusiveBuyMemberDTO.setInitTimes(exclusiveBuy.getInitTimes());
                memberDTOS.add(exclusiveBuyMemberDTO);
            }
            nftExclusiveBuyDTO.setMembers(memberDTOS);
            nftExclusiveBuyDTOS.add(nftExclusiveBuyDTO);
        }

        return nftExclusiveBuyDTOS;
    }
}
